﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace iBackupExplorer.Data
{
    class SmsDal : IDisposable
    {
        private static readonly SmsDal _smsDalInstance = new SmsDal();
        private readonly SmsContext5 _objEntities5;
        private readonly SmsContext6 _objEntities6;

        static SmsDal()
        {
        }

        private SmsDal()
        {
            _objEntities5 = new SmsContext5();
            _objEntities6 = new SmsContext6();
        }

        public static SmsDal GetInstance
        {
            get { return _smsDalInstance; }
        }

        #region IDisposable Members

        public void Dispose()
        {
            _objEntities5.Dispose();
            _objEntities6.Dispose();
            GC.SuppressFinalize(this);
        }
        #endregion
       public IList getAllSms5()
        {
            List<CustomSms> sms = (from items in _objEntities5.Messages5
                                   select new CustomSms { text = items.text, flags = items.flags, intDate =items.date, DiscutionId = items.group_id, PhoneNumber = items.address }).ToList();

            foreach (CustomSms items in sms)
            {

                AdressBook personn = AdressBookDal.GetInstance.getPersonn(items.PhoneNumber);
                if (personn != null)
                {
                    items.First = personn.First;
                    items.Last = personn.Last;
                }
                else
                {
                    items.First = items.PhoneNumber;
                    items.Last = string.Empty;
                }

                items.Date = Utils.Utils.ConvertFromUnix(items.intDate);
            }


            var result = (from final in sms
                          group final by final.DiscutionId
                              into g
                              orderby g.Key
                              select new { Sms = g.Key, Items = g, Info = g.First().First + " " + g.First().Last }).ToList();

            return result;

        }
        public IList getAllSms6()
        {
            List<CustomSms> sms = (from items in _objEntities6.Messages6
                                   select new CustomSms { text = items.text, flags = items.is_from_me, intDate =items.date, DiscutionId = items.handle_id, PhoneNumber = items.SmsHandle.id }).ToList();

            foreach (CustomSms items in sms)
            {

                AdressBook personn = AdressBookDal.GetInstance.getPersonn(items.PhoneNumber);

                if (items.flags == 1)
                    items.flags = 3;

                items.Date = Utils.Utils.ConvertFromMac(items.intDate);

                if (personn != null)
                {
                    items.First = personn.First;
                    items.Last = personn.Last;
                }
                else
                {
                    items.First = items.PhoneNumber;
                    items.Last = string.Empty;
                }
            }

            var result = (from final in sms
                          group final by final.DiscutionId
                              into g
                              orderby g.Key
                              select new { Sms = g.Key, Items = g, Info = g.First().First + " " + g.First().Last }).ToList();

            return result;

        }


        public long getPersonnId(long groupid)
        {
            var id = (from items in _objEntities5.Group_Member
                      where items.Group_id == groupid
                      select items.ROWID).FirstOrDefault();


            return id;

        }
    }
}
